package com.ckh.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckh.springboot.common.Constants;
import com.ckh.springboot.controller.dto.UserDTO;
import com.ckh.springboot.controller.dto.UserPasswordDTO;
import com.ckh.springboot.entity.Menu;
import com.ckh.springboot.entity.SysClass;
import com.ckh.springboot.entity.User;
import com.ckh.springboot.exception.ServiceException;
import com.ckh.springboot.mapper.RoleMapper;
import com.ckh.springboot.mapper.RoleMenuMapper;
import com.ckh.springboot.mapper.UserMapper;
import com.ckh.springboot.service.IMenuService;
import com.ckh.springboot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckh.springboot.utils.TokenUtils;
import com.ckh.springboot.vo.GetStudentClockInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ckh
 * @since 2022-04-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private static final Log LOG = Log.get();

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private IMenuService menuService;


    @Override
    public User register(UserDTO userDTO) {
        User one = getUserInfo(userDTO);
        if(one == null){
            one = new User();
            BeanUtil.copyProperties(userDTO, one, true);
            save(one);     //把copy完之后的用户对象存储到数据库
        }else{
            throw new ServiceException(Constants.CODE_600,"用户名已存在");
        }

        return null;
    }

    @Override
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        int update = userMapper.updatePassword(userPasswordDTO);
        if (update < 1) {
            throw new ServiceException(Constants.CODE_600, "密码错误");
        }
    }

    @Override
    public Page<User> findPage(Page<User> page, String username) {
       return userMapper.findPage(page, username);
    }

    @Override
    public List<GetStudentClockInfoVo> getStudentClockInfo(Integer classId) {
        return userMapper.getStudentClockInfo(classId);
    }

    @Override
    public Page<User> getUserList(Page<User> page,String name) {
        return userMapper.getUserList(page,name);
    }


    public  User getUserInfo(UserDTO userDTO){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper);    // 从数据库查询用户信息
        } catch(Exception e){
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        return one;
    }

    /**
     * 获取当前角色的菜单列表
     * @param roleFlag
     * @return
     */
    private List<Menu> getRoleMenus(String roleFlag){
        Integer roleId = roleMapper.selectByFlag(roleFlag);
        //当前角色所有菜单id集合
        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);
        // 查出系统所有菜单
        List<Menu> menus = menuService.findMenus("");
        // new一个最后筛选完成后的菜单
        List<Menu> roleMenus = new ArrayList<>();
        // 筛选当前用户角色菜单
        for (Menu menu : menus){
            if (menuIds.contains(menu.getId())) {
                roleMenus.add(menu);
            }
            List<Menu> children = menu.getChildren();
            // 移除children里不在menuIds集合中的元素
            children.removeIf(child -> !menuIds.contains(child.getId()));

        }
        return roleMenus;
    }
}
